<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/3/23
 * Time: 15:58
 */

namespace app\common\lib;


class Arr
{
    /**
     * 无限级分类
     * @param $data
     * @param int $pid
     * @param int $level
     * @return array
     */
    public static function tree($data, $pid = 0, $level = 0)
    {
        static $arr = [];
        foreach ($data as $key => $value) {
            if ($value['pid'] == $pid) {   //找出第一个顶级分类
                $value['level'] = $level;//给顶级分类赋一个层级
                $arr[] = $value;           //将顶级分类放入空数组中
                self::tree($data, $value['id'], $level + 1);//将所有分类进行一个排序
            }
        }

        return $arr;
    }
}